package cn.hrlly.tao2shou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.zip.Inflater;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.domain.Commodities;
import cn.hrlly.tao2shou.utils.ImageUtil;

/**
 * User: gengliang
 * Date: 2016-02-06
 * Time: 13:45
 */

public class CommoditiesAdapter extends BaseAdapter {

    private ArrayList<Commodities> list;
    private LayoutInflater mInflate;
    private ImageLoader imageLoader=ImageLoader.getInstance();

    public CommoditiesAdapter(ArrayList<Commodities> list,Context context) {
        this.list = list;
        mInflate=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflate.inflate(R.layout.layout_commodities_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Commodities commodities =list.get(position);
        if (null != commodities) {
            holder.tvCommodityName.setText(commodities.getCommodityName());
            holder.tvPrice.setText(String.valueOf(commodities.getCommodityPrice()));
            holder.tvDistance.setText(String.valueOf(commodities.getDistance()));
            holder.tvSchool.setText(commodities.getSchool());
            imageLoader.displayImage(commodities.getCommodityPhoto(),holder.ivCommodityPhoto, ImageUtil.getOptions());
            if(commodities.getBarterFlag()==0)
                holder.ivBarterFlag.setVisibility(View.VISIBLE);
        }
        return convertView;
    }

    class ViewHolder{
        @Bind(R.id.tvCommodityName)
        TextView tvCommodityName;
        @Bind(R.id.tvDistance)
        TextView tvDistance;
        @Bind(R.id.tvPrice)
        TextView tvPrice;
        @Bind(R.id.tvSchool)
        TextView tvSchool;
        @Bind(R.id.ivCommodityPhoto)
        ImageView ivCommodityPhoto;
        @Bind(R.id.ivBarterFlag)
        ImageView ivBarterFlag;

        public ViewHolder(View view){
            ButterKnife.bind(this,view);
        }
    }
}
